package cn.ecnu.note.controller;

import cn.ecnu.note.service.CommentService;
import cn.ecnu.note.vo.AdminCommentVo;
import cn.ecnu.note.vo.CommentFrontVo;
import cn.ecnu.note.vo.CommentVo;
import cn.ecnu.note.vo.ViewCommentVo;
import cn.ecnu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    //TODO: @Own(permission = Permission.COMMENT)
    @PostMapping("/add")
    public R addComment(@RequestBody CommentFrontVo vo) {
        CommentVo comment = commentService.addComment(vo);
        return R.ok().put("data", comment);
    }


    @GetMapping("/all")
    public R getAllComment(@RequestParam(value = "uid", required = false) Integer uid,
                           @RequestParam(value = "nid", required = false) Integer nid,
                           @RequestParam(value = "pid", required = false) Integer pid) {
        List<AdminCommentVo> result = commentService.getAllComment(uid, nid, pid);
        return R.ok().put("data", result);
    }







    // TODO: @Own(permission = Permission.VIEW)
    @GetMapping("/delete/{commentId}")
    public R deleteCommentById(@PathVariable("commentId") Integer commentId,
                               @RequestHeader("userId") Integer userId,
                               @RequestHeader("permission") Integer permission) {
        commentService.deleteById(commentId, userId, permission);
        return R.ok();
    }

    //TODO: @Own(permission = Permission.VIEW)
    @GetMapping("/getCommentByNoteId/{noteId}")
    public R getCommentByNoteId(@PathVariable("noteId") Integer noteId,
                                @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        ViewCommentVo vo = commentService.getCommentByNoteId(noteId, pageNum);
        return R.ok().put("data", vo);
    }

    // TODO: @Own(permission = Permission.VIEW)
    @GetMapping("/getCommentByUserId/{userId}")
    public R getCommentByUserId(@PathVariable("userId") Integer userId,
                                @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        ViewCommentVo vo = commentService.getCommentByUserId(userId, pageNum);
        return R.ok().put("data", vo);
    }




}
